package com.yaozheng.vocationaltraining.view.paper;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.paper.ItemQuestionListAdapter;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

/**
 * 一项题目ListView
 */
@EViewGroup(R.layout.item_question_list_view)
public class ItemQuestionListView extends LinearLayout {
    @ViewById
    ListView listView;

    ItemQuestionListAdapter itemQuestionListAdapter;

    ItemQuestionTitleView itemQuestionTitleView;

    public ItemQuestionListView(Context context) {
        super(context);
    }

    public ItemQuestionListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void loadData(int position, JSONObject itemData) {
        if (itemQuestionListAdapter == null) {
            itemQuestionListAdapter = new ItemQuestionListAdapter(itemData, getContext());
            itemQuestionTitleView = ItemQuestionTitleView_.build(getContext());
            listView.addHeaderView(itemQuestionTitleView);
            listView.setAdapter(itemQuestionListAdapter);
        } else {
            itemQuestionListAdapter.setItemData(itemData);
            itemQuestionListAdapter.notifyDataSetChanged();
        }
        itemQuestionTitleView.setContentValue(TypeUtils.getJsonString(itemData, "subject"));
    }
}
